/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastrarCliente.java
 *
 * Created on 27/04/2010, 13:47:26
 */

package fronteira;

import java.awt.Color;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mateus
 */
public class TelaVendas extends javax.swing.JInternalFrame {

    private DecimalFormat formata;
    private double valorTotal;

    public TelaVendas() {
        this.getContentPane().setBackground(Color.WHITE);
        initComponents();
        box.add(box_vista);
        box.add(box_prazo);
        box.add(box_cartao);
        box.add(box_cheque);
        formata = new DecimalFormat();
        this.valorTotal = 0;
    }

    public void limpaTela(){
        tela_valorTotal.setText("0.00");
        box.clearSelection();
       while(this.tela_tabela.getRowCount()>0)
             ((DefaultTableModel)this.tela_tabela.getModel()).removeRow(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        box = new javax.swing.ButtonGroup();
        concluir_venda = new javax.swing.JButton();
        cadcliente_cancelar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tela_tabela = new javax.swing.JTable();
        jLabel11 = new javax.swing.JLabel();
        jPanel16 = new javax.swing.JPanel();
        jLabel127 = new javax.swing.JLabel();
        tela_valorTotal = new javax.swing.JLabel();
        tela_remover_produtos = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        box_vista = new javax.swing.JRadioButton();
        box_cartao = new javax.swing.JRadioButton();
        box_prazo = new javax.swing.JRadioButton();
        box_cheque = new javax.swing.JRadioButton();
        tela_adicionar_produtos1 = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setTitle("Vendas");
        setMaximumSize(new java.awt.Dimension(810, 603));
        setVisible(true);

        concluir_venda.setText("Concluir Venda");
        concluir_venda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                concluir_vendaActionPerformed(evt);
            }
        });

        cadcliente_cancelar.setText("Cancelar");
        cadcliente_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cadcliente_cancelarActionPerformed(evt);
            }
        });

        jPanel2.setBackground(new java.awt.Color(153, 204, 255));
        jPanel2.setForeground(new java.awt.Color(51, 102, 255));

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/64x64/vendas.png"))); // NOI18N

        jPanel1.setBackground(new java.awt.Color(153, 204, 255));
        jPanel1.setForeground(new java.awt.Color(51, 102, 255));

        jLabel8.setText("Fechar");

        jLabel13.setFont(new java.awt.Font("Arial Black", 1, 36));
        jLabel13.setText("VENDAS");

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/64x64/fechar.png"))); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(131, 131, 131)
                .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 332, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addGap(28, 28, 28))
                    .addComponent(jButton5))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8))
                    .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        tela_tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Produto", "Quantidade", "Preço"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Double.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tela_tabela);

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel11.setText("Lista de Compras:");

        jPanel16.setBackground(new java.awt.Color(0, 102, 255));

        jLabel127.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel127.setForeground(new java.awt.Color(255, 255, 255));
        jLabel127.setText("TOTAL:   R$");

        tela_valorTotal.setFont(new java.awt.Font("Tahoma", 1, 24));
        tela_valorTotal.setForeground(new java.awt.Color(255, 255, 255));
        tela_valorTotal.setText("10.00");

        javax.swing.GroupLayout jPanel16Layout = new javax.swing.GroupLayout(jPanel16);
        jPanel16.setLayout(jPanel16Layout);
        jPanel16Layout.setHorizontalGroup(
            jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel16Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabel127)
                .addGap(18, 18, 18)
                .addComponent(tela_valorTotal)
                .addContainerGap(13, Short.MAX_VALUE))
        );
        jPanel16Layout.setVerticalGroup(
            jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel16Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel16Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tela_valorTotal)
                    .addComponent(jLabel127))
                .addContainerGap())
        );

        tela_remover_produtos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/32x32/excluir.png"))); // NOI18N
        tela_remover_produtos.setText("Remover Produto");
        tela_remover_produtos.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        tela_remover_produtos.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        tela_remover_produtos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tela_remover_produtosActionPerformed(evt);
            }
        });

        jLabel2.setText("Selecione a Forma de Pagamento:");

        box_vista.setText("Vista");

        box_cartao.setText("Cartão");

        box_prazo.setText("Prazo");

        box_cheque.setText("Cheque");

        tela_adicionar_produtos1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/32x32/adicionar.png"))); // NOI18N
        tela_adicionar_produtos1.setText("Adicionar Produto");
        tela_adicionar_produtos1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        tela_adicionar_produtos1.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        tela_adicionar_produtos1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tela_adicionar_produtos1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 495, Short.MAX_VALUE)
                .addComponent(tela_remover_produtos)
                .addGap(5, 5, 5)
                .addComponent(tela_adicionar_produtos1)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(box_vista)
                                    .addComponent(box_prazo))
                                .addGap(41, 41, 41)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(box_cartao)
                                    .addComponent(box_cheque))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 486, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cadcliente_cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(concluir_venda, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jPanel16, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 906, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tela_remover_produtos)
                        .addComponent(tela_adicionar_produtos1))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addGap(3, 3, 3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(box_vista)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(box_prazo))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(box_cartao)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(box_cheque)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel16, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cadcliente_cancelar)
                            .addComponent(concluir_venda))
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void concluir_vendaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_concluir_vendaActionPerformed
        // TODO add your handling code here:
        double valor_a_pagar = Double.parseDouble(tela_valorTotal.getText());
        if(valor_a_pagar == 0){
            JOptionPane.showMessageDialog(null,"Você não adicionou nenhum produto a lista de compras!", "Mensagem de Erro", JOptionPane.ERROR_MESSAGE);
        }
        else{
            if(box_vista.isSelected()){
                TelaVendaVista vista = new TelaVendaVista(valor_a_pagar);
                TelaPrincipal.desktop.add(vista);
                vista.setVisible(true);
            }
            else if(box_prazo.isSelected()){
                TelaVendaPrazo prazo = new TelaVendaPrazo(valor_a_pagar);
                TelaPrincipal.desktop.add(prazo);
                prazo.setVisible(true);
            }
            else if(box_cartao.isSelected()){
                TelaVendaCartao cartao = new TelaVendaCartao(valor_a_pagar);
                TelaPrincipal.desktop.add(cartao);
                cartao.setVisible(true);
            }
            else if(box_cheque.isSelected()){
                TelaVendaCheque cheque = new TelaVendaCheque(valor_a_pagar);
                TelaPrincipal.desktop.add(cheque);
                cheque.setVisible(true);
            }
        }
        this.limpaTela();
}//GEN-LAST:event_concluir_vendaActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
}//GEN-LAST:event_jButton5ActionPerformed

    private void tela_remover_produtosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tela_remover_produtosActionPerformed
        // TODO add your handling code here:
        int[] l = tela_tabela.getSelectedRows();
        DefaultTableModel tabela = (DefaultTableModel)tela_tabela.getModel();

        for(int i = (l.length-1); i >= 0; --i){
            this.valorTotal -= (Double) tabela.getValueAt(i, 3);
            tabela.removeRow(l[i]);
        }
        this.tela_valorTotal.setText(String.valueOf(formata.format(this.valorTotal)));
    }//GEN-LAST:event_tela_remover_produtosActionPerformed

    private void cadcliente_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cadcliente_cancelarActionPerformed
        // TODO add your handling code here:
        this.limpaTela();
    }//GEN-LAST:event_cadcliente_cancelarActionPerformed

    private void tela_adicionar_produtos1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tela_adicionar_produtos1ActionPerformed
        // TODO add your handling code here:
        TelaBuscaProduto tela_busca = new TelaBuscaProduto();
        TelaPrincipal.desktop.add(tela_busca);
        tela_busca.setVisible(true);

        this.valorTotal = 0;
        DefaultTableModel tabela = (DefaultTableModel)tela_tabela.getModel();
        tabela.addRow(new Object[]{9999,"Remedio",10,10.00});

        for(int i=0; i<this.tela_tabela.getRowCount(); i++)
            this.valorTotal += (Double) tabela.getValueAt(i, 3);

        this.tela_valorTotal.setText(String.valueOf(formata.format(this.valorTotal)));
        
    }//GEN-LAST:event_tela_adicionar_produtos1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup box;
    private javax.swing.JRadioButton box_cartao;
    private javax.swing.JRadioButton box_cheque;
    private javax.swing.JRadioButton box_prazo;
    private javax.swing.JRadioButton box_vista;
    private javax.swing.JButton cadcliente_cancelar;
    private javax.swing.JButton concluir_venda;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel127;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel16;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton tela_adicionar_produtos1;
    private javax.swing.JButton tela_remover_produtos;
    private javax.swing.JTable tela_tabela;
    private javax.swing.JLabel tela_valorTotal;
    // End of variables declaration//GEN-END:variables

}
